package traveldream.ejb.sessionBean;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;



import traveldream.ejb.DTO.*;
import traveldream.ejb.interfaces.registrationBeanLocal;
import traveldream.ejb.entities.*;


/**
 * Session Bean implementation class registrationBean
 */
@Stateless
public class registrationBean implements registrationBeanLocal {
	@PersistenceContext
	private EntityManager em;

	@Resource
	private EJBContext context;
    /**
     * Default constructor. 
     */
    public registrationBean() {
        // TODO Auto-generated constructor stub
    }
    
    @Override
    public String register(RegistrationDTO reDTO){
    	if(em.find(User.class, reDTO.getIdUser()) == null){
    		User u = new User(reDTO);
    		List<Group> groups = new ArrayList<Group>();
    		groups.add(Group.USER);
    		u.setGroups(groups);
    		em.persist(u);
    		return "/index.xhtml?faces-redirect=true";
    	}
    	return "registration?faces-redirect=true";
    }

}
